package com.zzty.springmybatis.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zzty.springmybatis.DAO.TeamExamDAO;
import com.zzty.springmybatis.DAO.UserDAO;
import com.zzty.springmybatis.entity.TeamExam;
import com.zzty.springmybatis.entity.User;
import com.zzty.springmybatis.service.TeamExamService;

@Service
public class TeamExamServiceImpl implements TeamExamService {

	@Autowired
	TeamExamDAO teamExamDao;

	@Autowired
	UserDAO userDao;

	public String[] getQuestionID(String date, Integer clazzId) {
		TeamExam te = teamExamDao.getInfoByDateAndClazzId(date, clazzId);
		String questions = te.getQuestions();
		String[] array = questions.split("\\|");
		return array;
	}

	public Integer searchExamId(String date, Integer clazzId) {
		TeamExam te = teamExamDao.getInfoByDateAndClazzId(date, clazzId);
		if (te == null) {
			return null;
		}
		Integer examId = te.getId();
		return examId;
	}

	public Integer searchClazzId(String name) {
		User user = new User();
		user.setName(name);
		User user1 = userDao.selectUserInfo(user);
		return user1.getClazzId();
	}

	public void addTeamExam(TeamExam te) {
		Integer id = teamExamDao.getNextId();
		te.setId(id);
		teamExamDao.addTeamExam(te);
	}

	public void updateWinners(Integer examId, Integer winnerId1, Integer winnerId2) {
		TeamExam te = new TeamExam();
		te.setId(examId);
		te.setWinnerId1(winnerId1);
		te.setWinnerId2(winnerId2);
		teamExamDao.updateWinnerIds(te);
	}

	public List<TeamExam> searchTeamExamByClazzId(Integer clazzId, Integer page) {
		return teamExamDao.searchTeamExamByClazzId(clazzId, page);
	}

	@Override
	public List<TeamExam> searchTeamExamByClazzId(Integer clazzId) {
		return teamExamDao.searchTeamExamOnlyByClazzId(clazzId);
	}

	public Integer searchNewId(String date, String questions, int clazzId) {
		return teamExamDao.searchNewId(date, questions, clazzId);
	}

	@Override
	public List<TeamExam> searchTeamExamByWinnerIdAndClazzId(Integer clazzId) {

		return teamExamDao.searchTeamExamByWinnerIdAndClazzId(clazzId);
	}

	@Override
	public List<TeamExam> searchTenTeamExamByClazzId(Integer clazzId, Integer page) {

		return teamExamDao.searchTenTeamExamByClazzId(clazzId, page);
	}

}
